package com.ve.downloader;

import com.ve.downloader.utils.HexUtils;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Utils {

    public static boolean inArray(Object o, Object[] arr) {
        if (o == null || arr == null) {
            return false;
        }
        for (Object o1 : arr) {
            if (o.equals(o1)) {
                return true;
            }
        }
        return false;
    }

    public static boolean inArrayArray(byte[] o, byte[][] arr) {
        for (byte[] o1 : arr) {
            if (Arrays.equals(o, o1)) {
                return true;
            }
        }
        return false;
    }

    public static void sleep(double t) {
        try {
            Thread.sleep((long) (t * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static byte low(int a) {
        return (byte) (a & 0xff);
    }

    public static byte hight(int a) {
        return (byte) ((a >> 8) & 0xff);
    }

    public static int sum(byte[] arr) {
        int s = 0;
        for (byte b : arr) {
            s += b & 0xff;
        }
        return s;
    }

    public static byte[] getTail(byte[] arr, int count) {
        return Arrays.copyOfRange(arr, arr.length - count, arr.length);
    }

    public static byte[] removeTail(byte[] arr, int count) {
        return Arrays.copyOfRange(arr, 0, count);
    }



    public static byte[] sub(byte[] arr, int start, int end) {
        if (start < 0) {
            start = arr.length + start;
        }
        if (end < 0) {
            end = arr.length + end;
        }
        try {
            return Arrays.copyOfRange(arr, start, end);

        }catch (Exception e){
            System.err.printf(" Error sub(%s,%d,%d) \n", HexUtils.toString(arr),start,end);
            e.printStackTrace();
            return null;
        }
    }
    public static boolean in(byte b, byte... values) {
        for (byte value : values) {
            if (value == b) {
                return true;
            }
        }
        return false;
    }
    public static boolean between(byte b, byte min,byte max){
        return b>=min&&b<=max;
    }

    public static byte[] from(byte[] arr, int begin) {
        return Arrays.copyOfRange(arr, begin, arr.length);
    }

    public static byte[] data(ByteBuffer byteBuffer) {
        return Arrays.copyOfRange(byteBuffer.array(), 0, byteBuffer.position());
    }

    public static float sum2(byte[] arr, int end) {
        float s = 0;
        for (int i = 0; i < end; i += 2) {
            s += (arr[i] & 0xff) << 8;
            s += (arr[i + 1] & 0xff);
        }
        return s/8;
    }

    public static byte[] create(int len, byte value) {
        byte[] bytes = new byte[len];
        Arrays.fill(bytes, value);
        return bytes;
    }

    public static byte[] merg(byte[] bytes, byte[] model) {
        byte[] result = Arrays.copyOf(bytes, bytes.length + model.length);
        for (int i = bytes.length; i < result.length; i++) {
            result[i] = model[i - bytes.length];
        }
        return result;
    }

    public static int getInt(byte hig, byte low) {
        return ((hig&0xff)<<8)+(low&0xff);
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(sub(new byte[]{1, 2, 3, 4, 5, 6}, 1, -2)));
    }
}
